/* eslint-disable vue/valid-v-bind */
<template>
  <div class="app-container">
    <el-table
      ref="mutipleTable"
      class="tb-edit"
      :data="tableData.slice((page-1)*limit,page*limit)"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        fixed
        prop="uid"
        label="序号"
        sortable
        width="75"
      />
      <el-table-column
        prop="username"
        label="用户名"
      />
      <el-table-column
        prop="role"
        label="角色"
        :formatter="onLineRole"
      />
      <el-table-column
        prop="isLocked"
        label="账号锁"
        width="100"
        :filters="[{ text: 'open', value: 0 ,key: 0}, { text: 'lock', value: 1,key: 1 }]"
        :filter-method="filterTag"
        filter-placement="bottom-end"
      >
        <template slot-scope="{row}">
          <el-tag
            :type="row.isLocked == '0' ? 'primary' : 'info'"
            disable-transitions
          >
            <i :class="row.isLocked == 0 ? 'el-icon-unlock' : 'el-icon-lock'" />  {{ row.isLocked == 0 ? 'open' : 'lock' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button
            size="small"
            type="success"
            @click="handleUpdate(row)"
          ><span>编辑</span></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      width="400px"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
        style="margin-left:50px; margin-right:50px;"
      >
        <el-form-item
          label="序号"
          prop="uid"
        >
          <el-input
            v-model="temp.uid"
            disabled
          />
        </el-form-item>
        <el-form-item
          label="用户名"
          prop="username"
        >
          <el-input v-model="temp.username" />
        </el-form-item>
        <el-form-item label="账号锁">
          <el-select v-model="temp.isLocked" class="filter-item" placeholder="Please select">
            <el-option v-for="item in statusOptions" :key="item.key" :label="item.label" :value="item.key" />
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-pagination
      :current-page="page"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { userList, updateUser } from '@/api/user'
export default {
  inject: ['reload'],
  data() {
    return {
      statusOptions: [{ label: 'open', key: '0' }, { label: 'lock', key: '1' }],
      roleOptions: [{ label: '业主', value: 'ROLE_PROPRIETOR' }, { label: '保安', value: 'ROLE_GUARD' }, { label: '物管', value: 'ROLE_MANAGEMENT' }],
      tableData: [],
      temp: { uid: '', username: '', isLocked: '' },
      rules: {
        username: [
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'change' }
        ],
        isLocked: [
          { required: true, message: '请选择是否锁定账户', trigger: ['blur', 'change'] }
        ]
      },
      multipleSelection: [],
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      dialogPvVisible: false,
      page: 1,
      limit: 10,
      total: null
    }
  },
  created() {
    this.getAllList()
  },
  methods: {
    getAllList() {
      userList().then(res => {
        this.tableData = res.data.users
        this.total = res.data.length
      })
    },
    onLineRole: function(row, column) {
      return row.role === 'ROLE_PROPRIETOR' ? '业主' : row.role === 'ROLE_GUARD' ? '保安' : '物管'
    },
    handleSizeChange: function(size) {
      this.pagesize = size
      console.log(this.pagesize) // 每页下拉显示数据
    },
    handleCurrentChange: function(currentPage) {
      this.page = currentPage
      console.log(this.currentPage) // 点击第几页
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    filterTag(value, row) {
      return row.isLocked === value
    },
    resetTemp() {
      this.temp = {
        uid: undefined,
        username: '',
        role: undefined,
        isLocked: null
      }
    },
    // 更新
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 修改
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateUser(this.temp).then((ref) => {
            ref.data = this.temp
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.reload()
          })
        }
      })
    },
    // 删除
    handleDelete(row, index) {
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    },
    // 删除
    deleteUser() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>

</style>
